#Alexander F. Gazmararian
#afg2@princeton.edu
#January 9, 2024

#Purpose: Prepare household debt to income ratio data for covariate used in subsequent analyses.

#Load packages
library(tidyverse)
library(tidylog)
library(here)

#FRBNY Consumer Credit Panel/Equifax, Bureau of Labor Statistics
#Load data
debt <- read.csv(here("data", "input", "household_debt", "household-debt-by-county.csv"))
#Rename
names(debt) <- c("year", "qtr", "fips", "low", "high")
#aggregate
debt <- debt %>%
  group_by(year, fips) %>%
  summarise(across(c(low,high), ~ mean(.x)))
#There's considerable missingness in the high estimates, so use the low estimates, which look equivalent
debt$debt <- debt$low
debt <- subset(debt, select = -c(low, high))
#Estimate the pre-2007 debt level--pre-treatment to avoid bias
debt07 <- subset(debt, year == 2006)
names(debt07)[3] <- "debt06"
debt07 <- subset(debt07, select = -year)
#Adjust FIPS codes for continuity
#24588    miami-dade      Florida 12025
miami <- debt07[debt07$fips==12086,]
miami$fips <- 12025
debt07 <- bind_rows(miami, debt07)
#27213 oglala lakota South Dakota 46102
oglala<-debt07[debt07$fips==46113,]
oglala$fips<-46102
debt07 <- bind_rows(oglala, debt07)
#Save output
saveRDS(debt07, here("data", "inter", "debt07.rds"))
